<template>
  <a-modal
    :title="dialogType == 'add' ? '新增' : dialogType=='edit' ? '编辑' : '详情'"
    width="50%"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="false">
      <a-form :form="form">
				<a-form-item
          label="库位名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['name', {rules: [{required: true, message: '请输入'}]}]" />
        </a-form-item>

        <a-form-item
          label="编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input 
						placeholder="请输入" 
						v-decorator="['code', {rules: [{required: true, message: '请输入！'}]}]" 
						:disabled="dialogType == 'edit'"
					/>
        </a-form-item>
				<!-- <a-form-item
          label="上级机构"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-tree-select
            v-decorator="['pid', {rules: [{ required: true, message: '请选择上级机构！' }]}]"
            style="width: 100%"
            :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
            :treeData="orgTree"
            placeholder="请选择上级机构"
            treeDefaultExpandAll
          >
            <span slot="title" slot-scope="{ id }">{{ id }}</span>
          </a-tree-select>
        </a-form-item> -->
				<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
        >
          <a-input-number placeholder="请输入" style="width: 100%" v-decorator="['sort', { initialValue: undefined ,rules: [{ required: true, message: '请输入！' }]}]" :min="1" :max="1000" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态"
        >
          <a-radio-group 
						v-decorator="['status', {rules: [{ required: true, message: '请选择！' }]}]"

					>
            <a-radio :value="0">正常</a-radio>
            <a-radio :value="1">禁用</a-radio>
          </a-radio-group>
        </a-form-item>

        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {
	getOrgPage,
	sysOrgAdd as add,
	sysOrgEdit as edit,
	updateStatus,  //切换状态
	// getOrgTree, //获取机构树
} from '@/api/modular/system/orgManage.js'
  export default {
		props:['materialType','pid'],
    data () {
      return {
				dialogType:'',
				id:'', //编辑的时候用
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
				orgTree: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
      }
    },
    methods: {
      // 初始化方法
      init (dialogType, record={}) {
				console.log('record', record)
				// this.getOrgTree()
				this.dialogType = dialogType
				this.id = record.id
        this.visible = true
				
				this.$nextTick(()=>{
					this.form.setFieldsValue({status: 0})  //设初始值
					// 编辑和详情需要回显
					if(dialogType!='add'){
						this.form.setFieldsValue(
							{
								name: record.name,
								// pid: record.pid,
								code: record.code,
								status: record.status,
								sort: record.sort,
							}
						)
					}
				})
      },
			 /**
       * 获取机构树，并加载于表单中
       */
			//  getOrgTree () {
      //   getOrgTree().then((res) => {
      //     this.formLoading = false
      //     if (!res.success) {
      //       this.orgTree = []
      //       return
      //     }
      //     this.orgTree = [{
      //       'id': '-1',
      //       'parentId': '0',
      //       'title': '顶级',
      //       'value': '0',
      //       'pid': '0',
      //       'children': res.data
      //     }]
      //   })
      // },
      handleSubmit () {
				let text = this.dialogType == 'edit' ? '编辑' : '新增'
				let api = this.dialogType=='add' ? add : edit
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
						let query = {
							...values,
							id: this.dialogType=='edit' ? this.id : undefined,
							pid:this.pid,
							unitRanId:'1440880080270487553', //类型写死为库位
							materialType: this.materialType
						}
            api(query).then((res) => {
              if (res.success) {
                this.$message.success(`${text}成功`)
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error(`${text}失败` + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
